package com.linkgie.galaxyframework.httpproxy.core;

/***
 * HTTP 请求的认证标头 （Http Authorization Header）；
 * <p>
 * 
 * 参考：https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Authorization ；
 * 
 * @author haiq
 *
 */
public class AuthorizationHeader extends com.linkgie.galaxyframework.httpproxy.HeaderEntry {
	
	/**
	 * HTTP 规范指定的认证头部；
	 */
	public static final String HEADER = "Authorization";

	/**
	 * 算法；
	 * <p>
	 * 例如：采用 HTTP Basic 认证时，此属性为“Basic”；
	 * 
	 * @see com.linkgie.galaxyframework.httpproxy.core.HttpAuthorizationAlgorithms
	 */
	private String algorithm;

	/**
	 * 主体；
	 * <p>
	 * 通常为用户名；
	 */
	private String principal;

	/**
	 * 凭证；
	 * <p>
	 * 通常为密码；
	 */
	private String credentials; 

//	public AuthorizationHeader() {
//	}

	public AuthorizationHeader(String principal, String credentials) {
		this(HttpAuthorizationAlgorithms.DEFAULT, principal, credentials);
	}

	public AuthorizationHeader(String algorithm, String principal, String credentials) {
		super(HEADER, buildValue(algorithm, principal, credentials));

		this.algorithm = algorithm;
		this.principal = principal;
		this.credentials = credentials;
	}

	public String getAlgorithm() {
		return algorithm;
	}

	/**
	 * 主体信息；
	 * 
	 * @return
	 */
	public String getPrincipal() {
		return principal;
	}

	public String getSecretKey() {
		return credentials;
	}

	private static String buildValue(String algorithm, String principal, String credentials) {
		StringBuilder authBuilder = new StringBuilder();
		authBuilder.append(algorithm).append(" ").append(principal).append(":").append(credentials);
		return authBuilder.toString();
	}

}
